package com.enjoy.book.biz;

import com.enjoy.book.bean.Book;
import com.enjoy.book.bean.Member;
import com.enjoy.book.bean.Record;
import com.enjoy.book.dao.BookData;
import com.enjoy.book.dao.MemberData;
import com.enjoy.book.dao.RecordData;
import com.enjoy.book.util.DBHelper;
import com.enjoy.book.util.DateHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 赵洋
 */
public class RecordBiz {
    RecordData recordData =new RecordData();
    BookData bookData =new BookData();
    MemberData memberData =new MemberData();
    public List<Record> getRecordByMemberId(long memberId)  {
    List<Record> records  =null;
        try {
            records = recordData.getRecordByMemberId(memberId);
            Member member = memberData.getById(memberId);
            for (Record record : records)
            {
                long bookId = record.getBookId();
                Book book = bookData.getById(bookId);
                record.setBook(book);
                record.setMember(member);
                java.sql.Date rentDate =record.getRentDate();
                long mills =rentDate.getTime();
                mills+=30*24*60*60*1000;
                java.sql.Date backDate= new java.sql.Date(mills);
                record.setBackDate(backDate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return records;

    }
   public int  add( List<Long> bookIds,long memberId,long userId){
       try {
           //1.启动事务
           DBHelper.beginTransaction();
           double total = 0;
           //2.拿到借阅的书籍编号
           for(long bookId: bookIds){
               //书籍编号

               //书籍对象
               Book book = bookData.getById(bookId);
               //调用价格
               double price = book.getPrice();
               double regPrice = price*0.3f;
               total += regPrice;
               //算押金
               //调用recordDao-->insert
               recordData.add(memberId,bookId,regPrice,userId);
               //调用bookDao --> update 数量
               bookData.modify(bookId,-1);

           }
           //调用memberDao -->update 余额
           memberData.modifyBalance(memberId,0-total);
           //.事务结束:
           DBHelper.commitTransaction();//事务提交:成功

       } catch (SQLException throwables) {
           throwables.printStackTrace();
           try {
               DBHelper.rollbackTransaction();//事务回滚:有异常
           } catch (SQLException e) {
               e.printStackTrace();
           }
           return 0;
       }
       return 1;
   }

    public int  modify(long memberId, List<Long> recordIds, long userId) {
        //1.开启事务
        try {
            DBHelper.beginTransaction();
            double total = 0;
            Member member = memberData.getById(memberId);
            for(long recordId:recordIds){
                //2.2 通过recordId 获取记录对象:书
                Record record = recordData.getById(recordId);
                //2.1 先累加押金
                //计算押金(逾期:超出1天扣1块)
                java.sql.Date  backDate = DateHelper.getNewDate(record.getRentDate(),30);
                //系统当前时间
                java.util.Date currentDate = new java.util.Date();
                int day = 0;
                if(currentDate.after(backDate)){
                    //计算押金
                    day = DateHelper.getSpan(currentDate,backDate);
                }
                total += record.getDeposit()-day;
                //2.2 更改record
                recordData.modify(day,userId,recordId);
                //2.3 修改书籍 +1
                bookData.modify(record.getBookId(),1);
            }
            //2。4修改余额
            memberData.modifyBalance(memberId,total);
            DBHelper.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                DBHelper.rollbackTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }

        return 1;

    }
   /* public List<Record> getRecordsByMemberId(long id) {

        return  null;
    }*/
}
